package cac.fenjian.resource.system.service;

import cac.fenjian.resource.system.dao.MenuDao;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;

@Service
public class SystemDbService {
    @Autowired
    private MenuDao menuDao;
    /**
     * 保存角色信息
     * */
    @Transactional(rollbackFor = Exception.class)
    public ResponseMsg saveRole(HashMap map){
        ResponseMsg responseMsg = new ResponseMsg();
        try{
            //判断当前角色code是否存在
            int cnt = menuDao.queryRoleById((String)map.get("id"));
            if(cnt>0){
                responseMsg.setRetCode("201");
                responseMsg.setMessage("当前录入的角色编码重复");
                return responseMsg;
            }
            menuDao.saveRole(map);
            responseMsg.setRetCode("200");
            responseMsg.setMessage("保存角色成功");
        }catch (Exception e){
            e.printStackTrace();
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201");
            responseMsg.setMessage("保存角色失败");
        }
        return responseMsg;
    }
    /**
     * 保存菜单权限信息
     * */
    @Transactional(rollbackFor = Exception.class)
    public ResponseMsg saveMenuRole(List<String> menus,HashMap role){
        ResponseMsg responseMsg = new ResponseMsg();
        try{
            //先删除当前角色下所有菜单权限
            menuDao.delMenuRole(role);
            for(String menu : menus){
                role.put("tsMenuId",menu);
                menuDao.insertRoleMenuRel(role);
            }
            responseMsg.setRetCode("200");
            responseMsg.setMessage("保存权限成功");
        }catch (Exception e){
            e.printStackTrace();
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201");
            responseMsg.setMessage("保存权限失败");

        }
        return responseMsg;
    }
    @Transactional(rollbackFor = Exception.class)
    public ResponseMsg delRole(HashMap role){
        ResponseMsg responseMsg = new ResponseMsg();
        try{
            menuDao.delRole(role);
            menuDao.delMenuRole(role);
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除角色成功");
        }catch (Exception e){
            e.printStackTrace();
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除角色失败");
        }
        return responseMsg;
    }

    //新增用户
    public int addUser(String account ,String password){
        String passWord = DigestUtils.md5Hex(password);
        return menuDao.addUser(account,passWord);
    }
    //删除用户
    public int delUser(String id){
        return menuDao.delUser(id);
    }

    //添加用户角色信息
    @Transactional(rollbackFor = Exception.class)
    public ResponseMsg insertUserRoleBatch(List<HashMap> userRoles){
        ResponseMsg responseMsg = new ResponseMsg();
        try{
            menuDao.insertUserRoleBatch(userRoles);
            responseMsg.setRetCode("200");
            responseMsg.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201");
            responseMsg.setMessage("保存失败");
        }
        return responseMsg;
    }
    @Transactional
    public ResponseMsg delRoleUser(List<HashMap> users){
        ResponseMsg responseMsg = new ResponseMsg();
        try{
            menuDao.delRoleUser(users);
            responseMsg.setRetCode("200");
            responseMsg.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            //手动设置回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201");
            responseMsg.setMessage("保存失败");
        }
        return responseMsg;
    }
}
